package group.igfd.manager.biz.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import group.igfd.manager.biz.dao.TagStyleDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import group.igfd.manager.biz.entity.TagStyleEntity;
import group.igfd.manager.biz.service.TagStyleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 风格表
 *
 * @author knight
 * @email sunlightcs@gmail.com
 * @date 2018-11-01 16:23:18
 */
@RestController
@RequestMapping("biz/tagStyle")
@Api(tags = {"A006/ 标签管理"})
public class TagStyleController {
    @Autowired
    private TagStyleService tagStyleService;

    @Autowired
    private TagStyleDao tagStyleDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("biz:tagStyle:list")
    @ApiOperation("获取风格标签信息")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tagStyleService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("biz:tagStyle:info")
    public R info(@PathVariable("id") Integer id) {
        TagStyleEntity tagStyle = tagStyleService.selectById(id);

        return R.ok().put("tagStyle", tagStyle);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("biz:tagStyle:save")
    public R save(@RequestBody TagStyleEntity tagStyle) {
        int i = tagStyleDao.checkTagStyle(null, tagStyle.getName());
        if (i > 0) {
            return R.error("标签已存在");
        }
        Date now = new Date();
        tagStyle.setCreatedAt(now);
        tagStyle.setUpdatedAt(now);
        tagStyleService.insert(tagStyle);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("biz:tagStyle:update")
    public R update(@RequestBody TagStyleEntity tagStyle) {
        int i = tagStyleDao.checkTagStyle(tagStyle.getId(), tagStyle.getName());
        if (i > 0) {
            return R.error("标签已存在");
        }
        tagStyle.setUpdatedAt(new Date());
        tagStyleService.updateById(tagStyle);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("biz:tagStyle:delete")
    public R delete(@RequestBody Integer[] ids) {
        tagStyleService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
